

class EventPositionController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ eventPositionInstanceList: EventPosition.list( params ), eventPositionInstanceTotal: EventPosition.count() ]
    }

    def show = {
        def eventPositionInstance = EventPosition.get( params.id )

        if(!eventPositionInstance) {
            flash.message = "EventPosition not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ eventPositionInstance : eventPositionInstance ] }
    }

    def delete = {
        def eventPositionInstance = EventPosition.get( params.id )
        if(eventPositionInstance) {
            try {
                eventPositionInstance.delete()
                flash.message = "EventPosition ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "EventPosition ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "EventPosition not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def eventPositionInstance = EventPosition.get( params.id )

        if(!eventPositionInstance) {
            flash.message = "EventPosition not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ eventPositionInstance : eventPositionInstance ]
        }
    }

    def update = {
        def eventPositionInstance = EventPosition.get( params.id )
        if(eventPositionInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(eventPositionInstance.version > version) {
                    
                    eventPositionInstance.errors.rejectValue("version", "eventPosition.optimistic.locking.failure", "Another user has updated this EventPosition while you were editing.")
                    render(view:'edit',model:[eventPositionInstance:eventPositionInstance])
                    return
                }
            }
            eventPositionInstance.properties = params
            if(!eventPositionInstance.hasErrors() && eventPositionInstance.save()) {
                flash.message = "EventPosition ${params.id} updated"
                redirect(action:show,id:eventPositionInstance.id)
            }
            else {
                render(view:'edit',model:[eventPositionInstance:eventPositionInstance])
            }
        }
        else {
            flash.message = "EventPosition not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def eventPositionInstance = new EventPosition()
        eventPositionInstance.properties = params
        def eventInstance = Event.get(params.id)
        return ['eventInstance':eventInstance, 'eventPositionInstance':eventPositionInstance]
    }

    def save = {
        def eventPositionInstance = new EventPosition(params)
        def eventInstance = Event.get(params.id)
        eventInstance.addToPositions(eventPositionInstance)
        if(!eventPositionInstance.hasErrors() && eventInstance.save()) {
            flash.message = "EventPosition ${eventPositionInstance.id} created"
  //          eventInstance.save()
            redirect(controller:'event', action:'myEvents')
        }
        else {
            render(view:'create',model:[eventPositionInstance:eventPositionInstance])
        }
    }
}
